package zhang.lao.service.console.base;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import zhang.lao.build.kit.LogKit;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.tool.BeanMapTool;
import zhang.lao.build.tool.MapTool;
import zhang.lao.dao.base.*;
import zhang.lao.build.tool.LzStringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import zhang.lao.build.mybatis.jdbc.auto.tool.ControllerQueryTool;
import zhang.lao.pojo.console.ConsoleContext;
import zhang.lao.pojo.console.req.BootStrapGridReq;
import zhang.lao.pojo.console.resp.BootStrapGridResp;
import zhang.lao.pojo.console.resp.HttpResultUtil;
import zhang.lao.pojo.console.resp.HttpResult;
import javax.annotation.Resource;
import java.util.List;
import zhang.lao.build.tool.UUIDTool;
import zhang.lao.service.console.MachineService;

/**
* 
* @author 
* @create 
**/
 @Service
public class WnMachineWorkReportGt06Service{
	@Resource
	private WnMachineWorkReportGt06Dao wnMachineWorkReportGt06Dao;
	@Resource
	private WnMachineDeviceDao wnMachineDeviceDao;
	@Resource
	private WnFarmDao wnFarmDao;
	@Resource
	private WnMachineDao wnMachineDao;
	@Resource
	private WnAppUserDao wnAppUserDao;
	@Resource
	private MachineService machineService;
	public String add(){
		return "console/wnMachineWorkReportGt06/wnMachineWorkReportGt06_form";
	}

	public String edit(ModelMap modelMap,java.lang.String id){
			modelMap.put("wnMachineWorkReportGt06", wnMachineWorkReportGt06Dao.selectByPrimaryKey(id));
		return "console/wnMachineWorkReportGt06/wnMachineWorkReportGt06_form";
	}

	public String list(){
		return "console/wnMachineWorkReportGt06/wnMachineWorkReportGt06_table";
	}

	public BootStrapGridResp json(BootStrapGridReq bootGridReq, ConsoleContext consoleContext){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}
		WnMachineWorkReportGt06Example wnMachineWorkReportGt06Example = new WnMachineWorkReportGt06Example();
 ControllerQueryTool.setWnMachineWorkReportGt06Criteria(bootGridReq.getQuery(), wnMachineWorkReportGt06Example.createCriteria()).andFarmIdIn(consoleContext.getFarms());
		List<WnMachineWorkReportGt06> wnMachineWorkReportGt06List = wnMachineWorkReportGt06Dao.selectByExample(wnMachineWorkReportGt06Example);
			List<JSONObject> list = BeanMapTool.objectsToJsonObjects(wnMachineWorkReportGt06List);
			if(list!=null) {
				for (JSONObject jsonObject : list) {
					jsonObject.put("farmName", wnFarmDao.selectByPrimaryKey(jsonObject.getString("farmId")).getName());
					jsonObject.put("deviceNumber", wnMachineDeviceDao.selectByPrimaryKey(jsonObject.getInteger("deviceId")).getDeviceNumber());
					WnMachine wnMachine = wnMachineDao.selectByPrimaryKey(jsonObject.getString("machineId"));
					if (wnMachine != null) {

						WnMachineAppUser wnMachineAppUser = machineService.getMachineAppUser(jsonObject.getString("id"));
						jsonObject.put("userPhoneNumber","未绑定");
						jsonObject.put("userName","未绑定");
						if(StringUtils.isNotBlank(wnMachineAppUser.getAppUserId())){
							WnAppUser wnAppUser = wnAppUserDao.selectByPrimaryKey(wnMachineAppUser.getAppUserId());
							if(wnAppUser!=null){
								jsonObject.put("userPhoneNumber",wnAppUser.getPhoneNumber());
								jsonObject.put("userName",wnAppUser.getNickName());
							}
						}
					jsonObject.put("userPhoneNumber", wnMachine.getLicensePlateNumber());

					} else {
						jsonObject.put("machineName", "未绑定农机");
					}

				}
			}

		return new BootStrapGridResp(page.getTotal(),list);
	}

	public HttpResult save(String formObjectJson){
		WnMachineWorkReportGt06 wnMachineWorkReportGt06= JSON.parseObject(formObjectJson,WnMachineWorkReportGt06.class);
			java.lang.String id=wnMachineWorkReportGt06.getId();
		if (id!=null) {
			wnMachineWorkReportGt06Dao.updateByPrimaryKeySelective(wnMachineWorkReportGt06);
		}else{
			wnMachineWorkReportGt06.setId(UUIDTool.getUUID());
			wnMachineWorkReportGt06Dao.insertSelective(wnMachineWorkReportGt06);
		}
		return HttpResultUtil.buildSuccess();

	}

	@Transactional
	public HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
		wnMachineWorkReportGt06Dao.deleteByPrimaryKey(java.lang.String.valueOf(id));
		}
		return HttpResultUtil.buildSuccess();
	}
}
